<template>
    <div class="box-card">
        <div class="card-header">
            <span>4G网络</span>
        </div>
        <div class="box-content">
            <el-row>
                <el-col :span="12" :xs="24" :sm="18" :md="18" :lg="8" :xl="8">
                    <el-form ref="form" :model="formData" :rules="formRules" label-width="120px" label-position="right">
                        <el-form-item label="IP" prop="ip">
                            <el-input v-model="formData.ip"></el-input>
                        </el-form-item>
                        <el-form-item label="端口" prop="port">
                            <el-input v-model="formData.port"></el-input>
                        </el-form-item>
                        <el-form-item label="IMEI" prop="imei">
                            <el-input v-model="formData.imei"></el-input>
                        </el-form-item>
                        <el-form-item label="ICCID" prop="iccid">
                            <el-input v-model="formData.rccid"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm">确定</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </div>
    </div>
    <dataCenter></dataCenter>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import dataCenter from "@/views/dataCenter/center/dataCenter.vue";

const form = ref(null)
const formData = ref({
    ip: '',
    port: '',
    imei: '',
    iccid: '',
});



const validateIP = (rule: any, value: string, callback: (error?: string) => void) => {
    if (!value) {
        callback();
    } else {
        const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
        if (!ipRegex.test(value)) {
            callback('请输入正确的IP地址');
        } else {
            callback();
        }
    }
};

const validatePort = (rule: any, value: string, callback: (error?: string) => void) => {
    if (!value) {
        callback();
    } else {
        const portRegex = /^\d{1,5}$/;
        if (!portRegex.test(value)) {
            callback('请输入正确的端口号');
        } else {
            callback();
        }
    }
};

const formRules = {
    ip: [
        { required: true, message: '请输入IP', trigger: 'blur' },
        { validator: validateIP, trigger: 'blur' },
    ],
    port: [
        { required: true, message: '请输入端口', trigger: 'blur' },
        { validator: validatePort, trigger: 'blur' },
    ],
    imei: [
        { required: true, message: '请输入IMEI', trigger: 'blur' },
    ],
    iccid: [
        { required: true, message: '请输入ICCID', trigger: 'blur' },
    ],
};

const submitForm = () => {
    form.value.validate((valid: boolean) => {
        if (valid) {
            // 表单验证通过，可以进行提交操作
            // ...
        } else {
            // 表单验证不通过，进行错误处理
            return false;
        }
    });
};

</script>

<style scoped lang></style>